package com.genesis.yunnanji.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.adapter.HouseTypeAdapter;
import com.genesis.yunnanji.bean.HouseDtailsBean;
import com.genesis.yunnanji.bean.HouseTypeBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.utils.NetWorkUtils;
import com.google.gson.Gson;
import com.jaeger.library.StatusBarUtil;
import com.ms.square.android.expandabletextview.ExpandableTextView;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.json.JSONObject;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;



public class HouseDetails extends BaseActivity {
    private String id;
    @ViewInject(R.id.tb_housedetails_toolbar)
    private Toolbar mToolbar;
    @ViewInject(R.id.iv_housedetails_bg)
    private ImageView backdrop;
    @ViewInject(R.id.tv_housedetails_name)
    private TextView tvName;
    @ViewInject(R.id.tv_housedetails_salestatus)
    private TextView tvSaleStatus;
    @ViewInject(R.id.tv_housedetails_opentime)
    private TextView tvOpen;
    @ViewInject(R.id.tv_housedetails_givetime)
    private TextView tvGive;
    private TagFlowLayout mFlowLayout;
    private LayoutInflater mInflater;
    @ViewInject(R.id.house_prince)
    private TextView tvPrince;
    @ViewInject(R.id.house_yushouzheng)
    private TextView tvYushou;
    @ViewInject(R.id.house_address)
    private TextView tvAddress;
    @ViewInject(R.id.house_kaifashang)
    private TextView tvCompany;
    @ViewInject(R.id.house_zhandi)
    private TextView tvZhandi;
    @ViewInject(R.id.house_jianzhu)
    private TextView tvJianzhu;
    @ViewInject(R.id.house_zhuangxiu)
    private TextView tvZhuangxiu;
    @ViewInject(R.id.house_shoulou)
    private TextView tvShoulou;
    @ViewInject(R.id.house_expan)
    private TextView expan;
    @ViewInject(R.id.house_elseintro)
    private LinearLayout llExpan;
    @ViewInject(R.id.house_hushu)
    private TextView tvHushu;
    @ViewInject(R.id.house_rongjilv)
    private TextView tvRongjilv;
    @ViewInject(R.id.house_wuye)
    private TextView tvWuye;
    @ViewInject(R.id.house_chanquan)
    private TextView tvChanquan;
    @ViewInject(R.id.house_lvhua)
    private TextView tvLvhua;
    @ViewInject(R.id.house_wuyefei)
    private TextView tvWuyefei;
    @ViewInject(R.id.expand_text_view)
    private ExpandableTextView intro;
    @ViewInject(R.id.mv_housedetails_mapview)
    private MapView mapView;
    @ViewInject(R.id.nsv_housedetails_scroll)
    private NestedScrollView scrollView;
    @ViewInject(R.id.rv_housedetails_huxingtu)
    private RecyclerView rvHousetype;
    @ViewInject(R.id.tv_housedetails_picnum)
    private TextView picnum;
    @ViewInject(R.id.tv_housedetails_map)
    private TextView tvMap;
    private AMap aMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.activity_housedetails);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        id = getIntent().getStringExtra("houseid");
        mInflater = LayoutInflater.from(context);
        StatusBarUtil.setColor(this, getResources().getColor(R.color.main_color), 2);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mFlowLayout = (TagFlowLayout) findViewById(R.id.id_flowlayout);
        mapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mapView.getMap();
        }
        getData();
        getHouseType();
    }

    @Override
    protected void setUpView() {
        expan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (llExpan.getVisibility() == View.VISIBLE) {
                    llExpan.setVisibility(View.GONE);
                    expan.setText("展开");
                } else {
                    llExpan.setVisibility(View.VISIBLE);
                    expan.setText("收起");
                }
            }
        });

    }

    HouseDtailsBean houseDetails;


    private void getData() {
        RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.HOUSE_DETAILS);
        params.addBodyParameter("id", id);
        NetWorkUtils.doCacheGet(params, new NetWorkUtils.HttpResultListener() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                Gson gson = new Gson();
                houseDetails = gson.fromJson(jsonObject.toString(), HouseDtailsBean.class);

                setData(houseDetails);

            }

            @Override
            public void OnCached(JSONObject jsonObject) {
            }

            @Override
            public void onFinished() {

            }

            @Override
            public void onError(String errorStr) {

            }
        });
    }


    private void setData(HouseDtailsBean data) {
        final HouseDtailsBean.ResultBean.InfoBean infoBean = data.getResult().getInfo();
        x.image().bind(backdrop, GenesisApiConfig.PIC_HOST + infoBean.getHouse_img());
        backdrop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HousePicWeb.class);
                intent.putExtra("url", infoBean.getPhoto_link());
                startActivity(intent);
            }
        });
        tvName.setText(infoBean.getHouse_title());
        if (infoBean.getSale_status().equals("待售")) {
            tvSaleStatus.setBackgroundColor(getResources().getColor(R.color.content));
            tvSaleStatus.setText(infoBean.getSale_status());
        } else {
            tvSaleStatus.setBackgroundColor(getResources().getColor(R.color.main_color));
            tvSaleStatus.setText(infoBean.getSale_status());
        }
        genesisUtils.setNotNullText(tvOpen, infoBean.getSale_time(), false);
        tvGive.setText("暂无数据");
        mFlowLayout.setAdapter(new com.zhy.view.flowlayout.TagAdapter<String>(infoBean.getHouse_tese()) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tag = (TextView) mInflater.inflate(R.layout.layout_textadapter, parent, false);
                tag.setText(infoBean.getHouse_tese().get(position));
                return tag;
            }
        });
        picnum.setText(infoBean.getPhotonums() + "张");
        tvPrince.setText(infoBean.getSale_price() + infoBean.getSale_unit());
        tvYushou.setText("预售证：" + infoBean.getSale_presel());
        tvAddress.setText("项目地址：" + infoBean.getHouse_addr());
        tvCompany.setText("开发商：" + infoBean.getCompany_name());
        tvZhandi.setText("占地面积：" + infoBean.getAreacov());
        tvJianzhu.setText("建筑面积：" + infoBean.getBuild());
        tvZhuangxiu.setText("装修情况：" + infoBean.getDecorate());
        tvShoulou.setText("售楼处地址：" + infoBean.getSale_addr());
        tvHushu.setText("规划户数：" + infoBean.getPlanum());
        tvRongjilv.setText("容积率：" + infoBean.getPlotrat());
        tvWuye.setText("物业公司：" + infoBean.getProcom());
        tvChanquan.setText("产权年限：" + infoBean.getPropr());
        tvLvhua.setText("绿化率：" + infoBean.getGreenrate());
        tvWuyefei.setText("物业费：" + infoBean.getProfee());
        intro.setText(infoBean.getContent());
        setupmap(infoBean.getLng(), infoBean.getLat());
        tvMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MapPoi.class);
                intent.putExtra("lat", infoBean.getLng());
                intent.putExtra("lng", infoBean.getLat());
                startActivity(intent);
            }
        });
    }


    public void setupmap(final Double lat, final Double lng) {
        LatLng latLng = new LatLng(lat, lng);
        aMap.addMarker(new MarkerOptions().position(latLng).draggable(false));
        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
        aMap.setOnMapTouchListener(new AMap.OnMapTouchListener() {
            @Override
            public void onTouch(MotionEvent motionEvent) {
                int action = motionEvent.getAction();
                switch (action) {
                    case MotionEvent.ACTION_UP:
                        scrollView.requestDisallowInterceptTouchEvent(false);
                        break;
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        scrollView.requestDisallowInterceptTouchEvent(true);
                        break;
                }
            }
        });

    }
    private void getHouseType() {
        RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.HOUSE_PHONE);
        params.addBodyParameter("id", id);
        NetWorkUtils.doCacheGet(params, new NetWorkUtils.HttpResultListener() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                Log.e("success", jsonObject.toString());
                Gson gson = new Gson();
                final HouseTypeBean houseTypeBean = gson.fromJson(jsonObject.toString(), HouseTypeBean.class);
                HouseTypeAdapter houseTypeAdapter = new HouseTypeAdapter(context, houseTypeBean.getResult().getList());
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
                linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                rvHousetype.setLayoutManager(linearLayoutManager);
                rvHousetype.setAdapter(houseTypeAdapter);
                houseTypeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        Intent intent = new Intent(context, HousePicWeb.class);
                        intent.putExtra("url", houseDetails.getResult().getInfo().getPhoto_link() + "?from = app");
                        startActivity(intent);
                    }
                });
            }

            @Override
            public void OnCached(JSONObject jsonObject) {
            }

            @Override
            public void onFinished() {

            }

            @Override
            public void onError(String errorStr) {

            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
}
